//
//  AgentViewController.m
//  EPBPro
//
//  Created by wucha on 2018/6/28.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "AgentViewController.h"
#import "AddOrgViewController.h"
#import "HttpOrgAction.h"
#import "OrgTableViewCell.h"
#import "AgentDetailViewController.h"

@interface AgentViewController ()
//@property (nonatomic,strong) NSArray *listArray;

@end

@implementation AgentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"代理商";
    if ([Utils getUserPermission:@"addorg"]) {
        UIButton *rightbutton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
        [rightbutton setTitle:@"+" forState:UIControlStateNormal];
        [rightbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [rightbutton addTarget:self action:@selector(addAgentAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightbutton];
        self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    }
    
    
    self.tableView.separatorStyle = 0;
    [self loadData];

}


-(void)loadData{
    if (self.tableView.mj_header) {
        [self.tableView.mj_header beginRefreshing];

    }else{
        __weak AgentViewController *filterView = self;
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [filterView loadHeader];
        }];
        header.lastUpdatedTimeLabel.hidden = YES;
        self.tableView.mj_header = header;
        
        [self.tableView.mj_header beginRefreshing];
        
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadFooter)];
        
        OrgActionInfo *orgActionInfo = [[OrgActionInfo alloc] init];
        orgActionInfo.orgType = @"1";
        [self setClass:@"HttpOrgAction" action:@selector(postSearchOrgList:block:) object:orgActionInfo];
    }
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self showWaitingViewWithMessage:@"加载中..." style:WaitingViewShowStyleFullSelfView];
//    OrgActionInfo *orgActionInfo = [[OrgActionInfo alloc] init];
//    orgActionInfo.orgType = @"1";
//    [HttpOrgAction postSearchOrgList:orgActionInfo block:^(id result, NSError *error) {
//        [self dismissWaitingView];
//        if (!error) {
//            self.listArray = result;
//            [self.tableView reloadData];
//        }
//    }];
}

-(void)deleteTheAgent:(NSString*)orgId{
    [HttpOrgAction deleteOrg:orgId block:^(id result, NSError *error) {
        if (!error) {
            [self showMessage:@"删除成功" autoDissmissAfterDelay:0];
            [self.tableView reloadData];

        }
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.resultArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    OrgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[OrgTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    OrgInfo *orgInfo = self.resultArray[indexPath.row];
    [cell setOrgInfoCell:orgInfo];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    //第二组可以左滑删除
//    if (indexPath.section == 2) {
//        return YES;
//    }
    
    return YES;
}

// 定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

// 进入编辑模式，按下出现的编辑按钮后,进行删除操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [BlockAlertView showAlertWithTitle:@"确定删除?" message:@"" alertViewStyle:UIAlertViewStyleDefault block:^(id clickView, NSInteger index) {
            if (index == 1) {
                OrgInfo *orgInfo = self.resultArray[indexPath.row];
                [self deleteTheAgent:orgInfo.id];
            }
        } cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//        if (indexPath.section == 2) {
//
//            //取消该演员的申请
//            NSString *user_no = [self.actor_cpllaboredArray[indexPath.row] valueForKey:@"user_no"];
//            [self fetch_api_Recruit_withdraw:user_no];
//
//        }
    }
}

// 修改编辑按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OrgInfo *orgInfo = self.resultArray[indexPath.row];

    AgentDetailViewController *agentDetailViewController = [[AgentDetailViewController alloc] initWithOrgInfo:orgInfo];
    [self.navigationController pushViewController:agentDetailViewController animated:YES];
    
}


-(void)addAgentAction:(id)sender{
    AddOrgViewController *addOrgViewController = [[AddOrgViewController alloc] initWithOrgType:OrgTypeAgent withBlock:^{
        [self loadData];

    }];
    [self.navigationController pushViewController:addOrgViewController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
